<?php
class FunctionsService extends Service {
    
    function __construct() {
        parent::__construct();
        $this->mDao = new FunctionsDao();
    }
    
    
    public function insertObject($obj){
        try {
            $newId = str_replace(" ", "_", strtolower($obj->getName()));
            $obj->setId($newId);
            if($this->mDao->getObject($obj->getId())!=null){
                throw new Exception("Entry with id [$newId] is Exist.");
            }
            return $this->mDao->insertObject($obj);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }

    public function updateObject($obj,$oldId){
        try {
            $newId = str_replace(" ", "_", strtolower($obj->getName()));
            $obj->setId($newId);
            if($obj->getId()!=$oldId){
                if($this->mDao->getObject($obj->getId())!=null){
                    throw new Exception("Duplicate entry for id [$newId].");
                }
            }
            return $this->mDao->updateObject($obj,$oldId);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }

    public function getList() {
        try {
            return $this->mDao->getList();
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function getObject($id){
        try {            
            return $this->mDao->getObject($id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }

    public function deleteObject($id){
        try {
            return $this->mDao->deleteObject($id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function getListByModule($module_id){
        try {
            return $this->mDao->getListByModule($module_id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function updateModuleId($moduleId,$oldModuleId){
        try {
            return $this->mDao->updateModuleId($moduleId,$oldModuleId);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
}
?>